/**
 * 
 */

package edu.umd.clip.lm.tools;

import java.io.*;

import edu.umd.clip.lm.factors.*;
import edu.umd.clip.lm.model.*;
import edu.umd.clip.lm.util.*;

import edu.berkeley.nlp.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class FLMFilter {

	public static class Options {
        @Option(name = "-input", required = false, usage = "Training data file (Default: stdin)")
		public String input;
        @Option(name = "-output", required = false, usage = "Output file (Default: stdout)")
		public String output;
        @Option(name = "-config", usage = "XML config file")
		public String config;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
        OptionParser optParser = new OptionParser(Options.class);
        Options opts = (Options) optParser.parse(args, true);

		Experiment.initialize(opts.config);
		Experiment experiment = Experiment.getInstance();

		FactorTupleDescription tupleDesc = experiment.getTupleDescription();

		FLMInputParser parser = new FLMInputParser(tupleDesc);

		try {
			BufferedReader reader = IO.getReader(opts.input == null ? System.in : IO.getInputStream((opts.input)));
		    BufferedWriter writer = IO.getWriter(opts.output == null ? System.out : IO.getOutputStream((opts.output)));

			for(String line = reader.readLine(); line != null; line = reader.readLine()) {
				long[] sentence = parser.parseSentence(line);
				
				if (sentence.length > 0) {
					writer.append(FactorTuple.toStringNoNull(sentence[0]));
					for(int i=1; i<sentence.length; ++i) {
						writer.append(' ');
						writer.append(FactorTuple.toStringNoNull(sentence[i]));
					}
				}
				writer.newLine();
			}
			reader.close();
			writer.flush();
			writer.close();
			
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}
